package com.xbongbong.paas.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.ItemDataPoJo;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.domain.entity.UpdateDataValueEntity;
import com.xbongbong.pro.domain.entity.UpdateSubDataEntity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/4/23 10:51
 * @since v1.0
 */
public class DataUtil {

    /**
     * 去重复
     * @param list
     * @return 去重结果
     */
    public static List<ItemDataPoJo> filterRepetition(List<ItemDataPoJo> list){
        List<ItemDataPoJo> resultList = new ArrayList<>();
        Set<Object> set = new HashSet<>();
        for(ItemDataPoJo itemDataPoJo:list){
            if (Objects.isNull(itemDataPoJo.getValue())) {
                continue;
            }
            Boolean isNotEmpty = StringUtil.isNotEmpty(itemDataPoJo.getValue().toString())
                    && (Objects.isNull(itemDataPoJo.getIsOther()) || Objects.equals(0, itemDataPoJo.getIsOther()));
            if(isNotEmpty && set.add(itemDataPoJo.getValue())){
                resultList.add(itemDataPoJo);
            }else if (Objects.equals(1, itemDataPoJo.getIsOther()) && set.add(itemDataPoJo.getValue())) {
                resultList.add(itemDataPoJo);
            }
        }
        return resultList;
    }

    public static List<UpdateDataEntity> getUpdateSubDataList(List<UpdateDataEntity> list) {
        List<UpdateDataEntity> resultList = new ArrayList<>();
        if (CollectionsUtil.isEmpty(list)) {
            return resultList;
        }
        list.forEach(item->{
            List<UpdateDataValueEntity> dataList = item.getData();
            if (CollectionsUtil.isNotEmpty(dataList)) {
                dataList.forEach(data->{
                    List<UpdateSubDataEntity> subDataList = data.getSubDataList();
                    if (Objects.nonNull(subDataList)) {
                        subDataList.forEach(subData->{
                            if(CollectionsUtil.isNotEmpty(subData.getSubDataList())) {
                                UpdateDataEntity updateDataEntity = new UpdateDataEntity();
                                Long subDataId = subData.getSubDataId();
                                Long parentId = subData.getParentId();
                                updateDataEntity.setAttr(subData.getAttr());
                                updateDataEntity.setFormId(subData.getFormId());
                                updateDataEntity.setId(subDataId);
                                updateDataEntity.setParentId(parentId);
                                updateDataEntity.setCorpid(item.getCorpid());
                                updateDataEntity.setData(subData.getSubDataList());
                                updateDataEntity.setUpdateTime(item.getUpdateTime());
                                resultList.add(updateDataEntity);
                            }
                        });
                    }

                });
            }
        });
        return resultList;
    }

    /**
     * 在data中移除子表单
     * @param list
     */
    public static void removeSubDataInData(List<UpdateDataEntity> list) {
        Iterator<UpdateDataEntity> iterator = list.iterator();
        while (iterator.hasNext()) {
            UpdateDataEntity updateDataEntity = iterator.next();
            List<UpdateDataValueEntity> data = updateDataEntity.getData();
            if(data == null){
                continue;
            }
            data.removeIf(updateDataValueEntity -> CollectionsUtil.isNotEmpty(updateDataValueEntity.getSubDataList()));
//            if (CollectionsUtil.isEmpty(data)) {
//                iterator.remove();
//            }
        }
    }

    public static List<JSONObject> getData(Map<String, JSONArray> subResultMap, Integer maxSize, JSONObject result) {
        List<JSONObject> list = new ArrayList<>();
        if (subResultMap.size() > 0) {
            for (int i = 0; i < maxSize; i++) {
                JSONObject dataJson = new JSONObject();
                dataJson.putAll(result);
                Iterator<String> stringIterator = subResultMap.keySet().iterator();
                while (stringIterator.hasNext()) {
                    String key = stringIterator.next();
                    int length = subResultMap.get(key).size();
                    if (i < length) {
                        // 避免溢出
                        dataJson.put(key, subResultMap.get(key).getJSONObject(i).get(key));
                    }
                }
                list.add(dataJson);
            }
        } else {
            list.add(result);
        }
        return list;
    }
}
